package com.shenma2009.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenma2009.domain.ResultResponse;
import com.shenma2009.pojo.TbProduct;
import com.shenma2009.service.TbProductService;
import com.shenma2009.mapper.TbProductMapper;
import com.shenma2009.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author 33509
* @description 针对表【tb_product(产品表)】的数据库操作Service实现
* @createDate 2023-04-25 16:28:49
*/
@Service
public class TbProductServiceImpl extends ServiceImpl<TbProductMapper, TbProduct>
    implements TbProductService{

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public ResultResponse add(TbProductVo tbProductVo) {
        QueryWrapper<TbProduct> tbProductQueryWrapper = new QueryWrapper<>();
        tbProductQueryWrapper.lambda().eq(TbProduct::getProductName,tbProductVo.getProductName()).last("limit 1");
        TbProduct one = this.getOne(tbProductQueryWrapper);
        if (one!=null){
            return ResultResponse.FAILED(500,"商品已存在");
        }
        TbProduct tbProduct = new TbProduct();
        BeanUtils.copyProperties(tbProductVo,tbProduct);
        this.save(tbProduct);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse del(TbProductIdVo tbProductIdVo) {
        TbProduct byId = this.getById(tbProductIdVo.getProductId());
        if (byId==null){
            return ResultResponse.FAILED(500,"商品不存在");
        }
        //删除
        this.removeById(tbProductIdVo.getProductId());
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse set(TbProductVo tbProductVo) {
        TbProduct tbProduct = new TbProduct();
        BeanUtils.copyProperties(tbProductVo,tbProduct);
        this.updateById(tbProduct);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse get(IdVo idVo) {
        TbProduct byId = this.getById(idVo.getId());
        if (byId==null){
            return ResultResponse.FAILED(500,"商品不存在");
        }
        return ResultResponse.SUCCESS(byId);
    }

    @Override
    public ResultResponse listByPage(PageVo pageVo) {
        Page<TbProduct> objectPage = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());
        Page<TbProduct> page = this.page(objectPage);
        List<TbProductVo> collect = page.getRecords().stream().map(item -> {
            TbProductVo tbProductVo = new TbProductVo();
            BeanUtils.copyProperties(item, tbProductVo);
            return tbProductVo;
        }).collect(Collectors.toList());
        Page<TbProductVo> tbProductVoPage = new Page<>();
        BeanUtils.copyProperties(page,tbProductVoPage);
        tbProductVoPage.setRecords(collect);
        return ResultResponse.SUCCESS(tbProductVoPage);
    }

    @Override
    public ResultResponse addBuyCar(BuyCarVo buyCarVo) {
        //构建key
        String key="BUY_CAR_"+buyCarVo.getBuyer();
        //放置购物车信息
        Boolean isExists=false;
        if (redisTemplate.hasKey(key)){
            //有购物车
            //取购物车的列表
            Set<Object> keys = redisTemplate.opsForHash().keys(key);
            for (Object item : keys) {
                String k = (String) item;
                Object o = redisTemplate.opsForHash().get(key, k);
                if (o != null) {
                    //有相同商品
                    if (k.equals(buyCarVo.getSku())) {
                        Integer count = (Integer) o;
                        count++;
                        isExists = true;
                        //保存到购物车里面
                        redisTemplate.opsForHash().put(key, k, count);

                    }
                }
            }
            if (!isExists) {
                redisTemplate.opsForHash().put(key,buyCarVo.getSku(),buyCarVo.getCount());
            }
        }else{
            //没有购物车
            //1.直接push
            redisTemplate.opsForHash().put(key,buyCarVo.getSku(),buyCarVo.getCount());
        }
        //返回成功
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse getBuyCar(IdVo idVo) {
//构建key
        String key="BUY_CAR_"+idVo.getId();
        if (!redisTemplate.hasKey(key)){
            return ResultResponse.FAILED(404,"没有购物车信息");
        }
        //--3
        Set<Object> keys = redisTemplate.opsForHash().keys(key);
        List<String> collect = keys.stream().map(item -> {
            return (String) item;
        }).collect(Collectors.toList());
        //根据集合获取商品
        QueryWrapper<TbProduct> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(TbProduct::getProductSpu,collect);
        List<TbProduct> list = this.list(wrapper);
        List<TbProductVo> Vocollect = list.stream().map(item -> {
            TbProductVo tbProductVo = new TbProductVo();
            BeanUtils.copyProperties(item, tbProductVo);
            return tbProductVo;
        }).collect(Collectors.toList());
        for (Object item : keys) {
            String sku=(String) item;
            Object o = redisTemplate.opsForHash().get(key, sku);
            Integer count=(Integer) o;
            for (TbProductVo vo :Vocollect){
                if(vo.getProductSpu().equals(sku)){
                    vo.setCount(count);
                }

            }
        }
        return ResultResponse.SUCCESS(Vocollect);
    }
}




